package com.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.Scroller;
import android.widget.TextView;

import com.util.LogUtil;
import com.util.ScreenUtil;

/**
 * Created by Administrator on 2016/6/27.
 */
public class SwipeItemLayout extends FrameLayout {
    private Scroller mScroller; //滑动类
    private float downX;
    private float downBoundaryX;
    private float slideDistanceTo;
    private float slideDistanceBy;
    private Context context;
    private TextView tv_delete;
    private FrameLayout container;
    private boolean isFirstAddView = true;
    private boolean isTouching = false;
    private boolean isAttachBoundary = false;

    public SwipeItemLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initSwipeItemLayout();
    }

    private void initSwipeItemLayout() {
        container = new FrameLayout(context);
        tv_delete = new TextView(context);
        tv_delete.setText("删除");
        tv_delete.setTextSize(20);
        tv_delete.setTextColor(context.getResources().getColor(android.R.color.white));
        tv_delete.setBackgroundColor(context.getResources().getColor(android.R.color.holo_red_light));
        tv_delete.setGravity(Gravity.CENTER);
        FrameLayout.LayoutParams parmas = new FrameLayout.LayoutParams(200, ViewGroup.LayoutParams.MATCH_PARENT);
        parmas.gravity = Gravity.RIGHT;
        container.addView(tv_delete, parmas);
        ViewTreeObserver observer = getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                getViewTreeObserver().removeGlobalOnLayoutListener(this);
                if (isFirstAddView) {
                    addView(container, new FrameLayout.LayoutParams(ScreenUtil.getScreenWidth() + 200, ViewGroup.LayoutParams.MATCH_PARENT));
                    isFirstAddView = false;
                }
            }
        });

        mScroller = new Scroller(context);
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
//                LogUtil.i("test", "onInterceptTouchEventACTION_DOWN");
                downX = ev.getX();
                break;
        }
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = event.getX();
//                LogUtil.i("test", "onTouchEventACTION_DOWN");
                break;
            case MotionEvent.ACTION_MOVE:
                slideDistanceTo = event.getX() - downX;
                LogUtil.i("test", "slideDistanceTo:" + slideDistanceTo);
                if(!isTouching){
                    if (slideDistanceTo <= -200) {
                        downBoundaryX = event.getX();
                        break;
                    }
                    if (slideDistanceTo >=0) {
                        break;
                    }
                    scrollTo((int) -slideDistanceTo, 0);
                }else{
                    if (slideDistanceTo <=0) {
                        break;
                    }
                    if (slideDistanceTo >=200) {
                        break;
                    }
                    scrollTo((int) -slideDistanceTo +200, 0);
                }
//                LogUtil.i("test", "ACTION_MOVE");
                break;
            case MotionEvent.ACTION_UP:
                if(Math.abs(getScrollX()) > 100){
                    smoothScrollTo(200);
                    isTouching = true;
                }else{
                    smoothScrollTo(0);
                    isTouching = false;
                }
//                LogUtil.i("test", "ACTION_UP");
                break;
        }
        return true;
    }

    private void smoothScrollTo(int destX) {
        int scrollX = getScrollX();
        int deltaX = destX - scrollX;
        mScroller.startScroll(scrollX, 0, deltaX, 0, 600);
        invalidate();
    }

    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()) {
            scrollTo(mScroller.getCurrX(), 0);
            invalidate();
        }
    }

    public void setOnItemDeleteClickListener(OnClickListener listener) {
        tv_delete.setOnClickListener(listener);
    }
}
